#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3f;
const int Mod = 998244353;
int qpow(int a, int b)
{
    int ret = 1;
    while (b > 0)
    {
        if (b & 1)
            ret = ret * a % Mod;
        a = a * a % Mod;
        b >>= 1;
    }
    return ret;
}

void solve()
{
    int n, d;
    cin >> n >> d;
    string s;
    cin >> s;
    unordered_map<char, int> mp;
    for (auto e : s) mp[e]++;
    int i = 0;
    while (1)
    {
        int x = d * i;
        string t = to_string(x);
        if (t.size() > n)
        {
            cout << -1 << endl;
            return;
        }
        bool flag = false;
        unordered_map<char, int> mpp = mp;
        for (int j = 0; j < t.size(); j++)
        {
            if (mpp[t[j]] <= 0)
            {
                flag = true;
                break;
            }
            mpp[t[j]]--;
        }
        if (!flag)
        {
            cout << x << endl;
            return;
        }
        i++;
    }
    cout << -1 << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _ = 1;
    // cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}